package net.xi.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xi.news.dao.SysDictDataMapper;
import net.xi.news.dao.SysDictTypeMapper;
import net.xi.news.pojo.SysDictData;
import net.xi.news.pojo.SysDictType;
import net.xi.news.service.RedisService;
import net.xi.news.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 系统变量
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 根据字典code和type查询value
     * @param type
     * @param code
     * @return
     */
    @Override
    public String getTypeById(String type,String code){
        String s = redisService.get("NEWS_TYPE_" + code + type);
        if (StringUtils.isEmpty(s)){
            //          这里要优化 先查询所有字典缓存下来
            LambdaQueryWrapper<SysDictType> queryWrapper=Wrappers.lambdaQuery();
            queryWrapper.select(SysDictType::getId).eq(SysDictType::getCode,code);
            SysDictType sysDictTypes = sysDictTypeMapper.selectOne(queryWrapper);
            LambdaQueryWrapper<SysDictData> queryWrapper1=Wrappers.lambdaQuery();
            queryWrapper1.select(SysDictData::getValue).eq(SysDictData::getTypeId,sysDictTypes.getId()).eq(SysDictData::getCode,type);
            SysDictData sysDictData = sysDictDataMapper.selectOne(queryWrapper1);
            redisService.set("NEWS_TYPE_"+code+type,60*60*60,sysDictData.getValue());
            return sysDictData.getValue();
        }else {
            return s;
        }

    }
}
